package org.pms.demo.webservice.controller;

import org.pms.demo.webservice.utils.ProjectUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;

/**
 * Created by xusaike on 17/5/31.
 */
@Component
@Path("/error")
public class ErrorMsgVersionRestController {
    /**
     *
     * @param user_agent
     * @param responese
     * @param request
     * @param key
     * @param headerChanel
     * @param appid
     * @return
     */
    @GET
    public String error(@HeaderParam("user-agent") String user_agent,
                           @Context HttpServletResponse responese, @Context HttpServletRequest request,@Context HttpHeaders headers,
                           @QueryParam("key")String key, @QueryParam("forceLogout")String forceLogout,
                           @HeaderParam("info-channel")String headerChanel, @HeaderParam("info-appid")String appid){
        System.out.println("error:-------------------");
        System.out.println(headers.getRequestHeaders());
        System.out.println(request.getCookies());
        System.out.println(request.getSession());

        java.util.Enumeration   e   =   request.getSession().getAttributeNames();

        if(request.getParameter("forceLogout") != null) {
            key="您已经被管理员强制退出，请重新登录";
        }

        while( e.hasMoreElements())   {
            String sessionName=(String)e.nextElement();
            System.out.println("\nsession "+sessionName+"="+request.getSession().getAttribute(sessionName));
        }
        return ProjectUtils.getErrorMsg(key);
    }
    @POST
    @Path("post")
    public String geterror(@HeaderParam("user-agent") String user_agent,
                        @Context HttpServletResponse responese, @Context HttpServletRequest request,@Context HttpHeaders headers,
                        @QueryParam("key")String key,
                        @HeaderParam("info-channel")String headerChanel, @HeaderParam("info-appid")String appid){
        System.out.println(headers.getRequestHeaders());
        System.out.println(request.getCookies());
        System.out.println(request.getSession());

        java.util.Enumeration   e   =   request.getSession().getAttributeNames();

        while( e.hasMoreElements())   {
            String sessionName=(String)e.nextElement();
            System.out.println("\nsession item name="+sessionName);
            System.out.println("\nsession item value="+request.getSession().getAttribute(sessionName));
        }
        return ProjectUtils.getErrorMsg(key);
    }
}
